var mongoose = require('./db.js')

var FocusSchema = mongoose.Schema({
  title: { 
    type: String, 
    trim: true 
  }, 
  pic: String,
  redirect: { 
    type: String, 
    set(url) {  // 增加数据的时候对redirect字段进行处理，返回的数据就是redirect在数据库实际保存的值
      if(!url) return url; 
      if(url.indexOf('http://')!=0 && url.indexOf('https://')!=0) { 
        url = 'http://' + url; 
      }
      return url; 
    }, 
    get(url) { // 不建议使用
      if(!url) return url; 
      if(url.indexOf('http://')!=0 && url.indexOf('https://')!=0) { 
        url = 'http://' + url; 
      }
      return url; 
    } 
  },
  status: {
    type: Number,
    default: 1 
  }
})

var FocusModel = mongoose.model('Focus', FocusSchema, 'focus')

module.exports = FocusModel